<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermission({pid:0,type:1})">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="addPermission({pid:row.id,type:0})">添加</el-button>
              <el-button type="text" @click="updataPermission(row.id)">编辑</el-button>
              <el-button type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 添加弹窗 -->
    <el-dialog :visible="showDialog" :title="formData.id?'编辑权限点':'添加权限点'" @close="closeDialog">
      <el-form ref="addForm" label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch v-model="formData.enVisible" active-text="启用" active-value="1" inactive-text="不启用" inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="publish">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermiissionListAPI, addPermissionAPI, getPermiissionAPI, updataPermiissionAPI } from '@/api/permission'
import transTree from '@/utils/transTree'
export default {
  data() {
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('权限标识不能为空~~~'))
      } else {
        // const codeArr = this.codeList.map(item => item.code)
        // if (this.formData.id) {
        //   const arr = codeArr.filter(item => item !== this.formData.code)
        //   if (arr.includes(value)) {
        //     callback(new Error('权限标识不能为重复~~~'))
        //   }
        // } else {
        //   if (codeArr.includes(value)) {
        //     callback(new Error('权限标识不能为重复~~~'))
        //   }
        // }
        // callback()
        const type1 = this.codeList.filter(item => item.type === 1)
        const type2 = this.codeList.filter(item => item.type === 0)
        if (this.formData.id) {
          // 编辑
          console.log(type1)
          const type1_ = type1.filter(item => item.code !== this.formData.code)
          const type2_ = type2.filter(item => item.code !== this.formData.code)
          if (this.formData.type === 1) {
            const codeArr = type1_.map(item => item.code)
            if (codeArr.includes(value)) {
              callback(new Error('权限标识不能为重复~~~'))
            }
            callback()
          } else {
            const codeArr = type2_.map(item => item.code)
            if (codeArr.includes(value)) {
              callback(new Error('权限标识不能为重复~~~'))
            }
            callback()
          }
        } else {
          // 添加
          if (this.formData.type === 1) {
            const codeArr = type1.map(item => item.code)
            if (codeArr.includes(value)) {
              callback(new Error('权限标识不能为重复~~~'))
            }
            callback()
          } else {
            const codeArr = type2.map(item => item.code)
            if (codeArr.includes(value)) {
              callback(new Error('权限标识不能为重复~~~'))
            }
            callback()
          }
        }
      }
    }
    return {
      // 页面渲染列表
      list: [],
      // 控制弹窗显示和隐藏
      showDialog: false,
      // 添加的表单
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      // 表单验证
      rules: {
        code: [{ validator: validateCode, trigger: 'blur' }]
      },
      // 权限标识数组
      codeList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取页面渲染列表
    async getList() {
      const res = await getPermiissionListAPI()
      this.list = transTree(res)
      this.codeList = res
    },
    // 打开弹窗
    addPermission({ pid, type }) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 关闭弹窗
    closeDialog() {
      this.showDialog = false
      this.resetFrom()
    },
    // 清空表单
    resetFrom() {
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
      this.$refs.addForm.resetFields()
    },
    // 提交表单
    publish() {
      this.$refs.addForm.validate(async(valid) => {
        if (!valid) return
        if (this.formData.id) {
          await updataPermiissionAPI(this.formData)
        } else {
          await addPermissionAPI(this.formData)
        }
        this.$message.success(`${this.formData.id ? '编辑' : '添加'}成功`)
        this.closeDialog()
        this.getList()
      })
    },
    // 获取权限点详情
    async updataPermission(id) {
      const res = await getPermiissionAPI(id)
      this.formData = res
      this.showDialog = true
    }
  }
}
</script>
